package com.rightsidetech.http.controller.admin.store;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.store.AddStoreGradeReq;
import com.rightsidetech.system.dtos.request.store.UpdateStoreGradeReq;
import com.rightsidetech.system.dtos.response.store.StoreGradeResp;
import com.rightsidetech.system.service.SysStoreGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Package: com.rightsidetech.http.controller.admin.store
 * @ClassName: SysStoreGradeController
 * @Author: yinj
 * @CreateTime: 2021/10/21 13:58
 * @Description: 门店等级相关
 */
@Slf4j
@RestController
@RequestMapping("/admin/grade")
@ShenyuSpringMvcClient(path = "/admin/grade/**")
@Api(tags = "门店等级相关api")
public class SysStoreGradeController extends BaseController {

    @Resource
    private SysStoreGradeService sysStoreGradeService;


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页")
    public ResponseData<PageInfo<StoreGradeResp>> page(PageRequest request){
        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysStoreGradeService.page(request);

    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/add")
    @ApiOperation("新增")
    @ModifyLog(serviceClass = SysStoreGradeService.class,modifyType = ModifyName.SAVE)
    public ResponseData add(@RequestBody AddStoreGradeReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysStoreGradeService.add(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("修改")
    @ModifyLog(serviceClass = SysStoreGradeService.class)
    public ResponseData update(@RequestBody UpdateStoreGradeReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysStoreGradeService.update(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除")
    @ApiOperationSupport(ignoreParameters = {"level", "levelName"})
    @ModifyLog(serviceClass = SysStoreGradeService.class)
    public ResponseData del(@RequestBody UpdateStoreGradeReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysStoreGradeService.del(req,loginInfoId);
    }



}
